package com.yunji.web.cabinet;

import com.yunji.aggregate.model.dto.container.CabinetParam;
import com.yunji.aggregate.model.vo.container.CabinetStatisticsVO;
import com.yunji.aggregate.model.vo.container.SpecificCabinetVO;
import com.yunji.aggregate.service.ContainerAggregateService;
import com.yunji.cabinet.business.CabinetBusiness;
import com.yunji.cabinet.cache.CabinetCache;
import com.yunji.cabinet.device.DeviceCommandEngine;
import com.yunji.cabinet.dto.CabinetCycleDTO;
import com.yunji.cabinet.dto.CabinetDTO;
import com.yunji.cabinet.dto.SetProductDTO;
import com.yunji.cabinet.params.SpecificUpgradeCabinetParam;
import com.yunji.cabinet.vo.CabinetCycleVO;
import com.yunji.cabinet.vo.SpecificUpgradeCabinetVO;
import com.yunji.common.annotation.Log;
import com.yunji.common.base.BaseController;
import com.yunji.common.base.response.Response;
import com.yunji.common.base.response.ResponseHelper;
import com.yunji.common.constant.LogicCode;
import com.yunji.common.enums.common.BusinessType;
import com.yunji.common.model.cache.CabinetCacheDTO;
import com.yunji.common.utils.ExcelUtil;
import com.yunji.system.context.SystemContext;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * @Package com.yunji.web.cabinet
 * FileName:	CabinetController.java
 * Copyright:	Copyright © 2024 云集互联 All Rights Reserved
 * Company:		云集互联
 * @version: 1.0
 * @since: JDK 1.8.0_202
 * Create at:   2024-03-30 09:31:06
 * Description:
 * 货柜管理交互
 * Modification History:
 * Author					Date						Version			Description
 * --------------------------------------------------------------------------------
 * HuaAo				2024-03-30 09:31:06			1.0				1.0 Version
 */
@Api(tags = "管理端-货柜管理")
@RestController
@RequestMapping("/container/container")
public class CabinetController extends BaseController {
    @Resource
    private CabinetBusiness business;

    @Resource
    private ContainerAggregateService aggregateService;

    @GetMapping("/cabinetStatus/{imei}")
    @ApiOperation("检测售卖柜是否在线")
    @PreAuthorize("@ss.hasPermi('container:container:statusInfo')")
    public Response<List<CabinetCycleVO>> cabinetStatus(@PathVariable String imei) {
        return ResponseHelper.handle(DeviceCommandEngine.online(imei), "售卖柜不在线");
    }

    @GetMapping("/getVersion/{imei}")
    @ApiOperation("获取货柜版本号")
    public Response<String> getVersion(@PathVariable String imei) {
        CabinetCacheDTO cabinet = CabinetCache.getCabinet(imei);
        return ResponseHelper.success(cabinet.getVersion());
    }

    @GetMapping("/cabinetStatusList")
    @ApiOperation("获取已经成功建立TCP的所有列表")
    public Response<?> cabinetStatusList() {
        return ResponseHelper.success(DeviceCommandEngine.cabinetStatusList());
    }

    @PostMapping("/cyclePageList")
    @ApiOperation("查询货柜在线离线记录")
    public Response<List<CabinetCycleVO>> cyclePageList(@Valid @RequestBody CabinetCycleDTO dto) {
        return ResponseHelper.successWithPage(business.cyclePageList(dto), dto.getTotal());
    }

    @PostMapping("/list")
    @PreAuthorize("@ss.hasPermi('container:container:query')")
    @ApiOperation("分页查询货柜")
    public Response<List<CabinetStatisticsVO>> search(@RequestBody @Valid CabinetParam cabinetParam) {
        cabinetParam.setBindSearch(LogicCode.NO);
        List<CabinetStatisticsVO> list = aggregateService.searchCabinet(cabinetParam);
        return ResponseHelper.successWithPage(list);
    }

    @PostMapping("/export")
    @PreAuthorize("@ss.hasPermi('container:container:export')")
    public void export(CabinetParam containerParam) {
        containerParam.setBindSearch(LogicCode.NO);
        List<CabinetStatisticsVO> list = aggregateService.searchCabinet(containerParam);
        ExcelUtil<CabinetStatisticsVO> util = new ExcelUtil<>(CabinetStatisticsVO.class);
        if (SystemContext.isSite())
            util.hideColumn("agentName");
        util.exportExcel(response, list, "货柜数据");
    }

    @PostMapping("/unbindList")
    @ApiOperation("未绑定货柜列表")
    public Response<List<CabinetStatisticsVO>> unbindList(@RequestBody CabinetParam param) {
        param.setBindSearch(LogicCode.YES);
        return ResponseHelper.successWithPage(aggregateService.searchCabinet(param));
    }

    @ApiModelProperty("货柜升级专用查询")
    @GetMapping("/specificUpgradeList")
    public Response<List<SpecificUpgradeCabinetVO>> specificUpgradeList(SpecificUpgradeCabinetParam param) {
        return ResponseHelper.successWithPage(business.specificUpgradeList(param), param.getTotal());
    }

    @PostMapping("/unbindExport")
    @ApiOperation("未绑定货柜导出")
    public void unbindExport(@RequestBody CabinetParam param) {
        param.setBindSearch(LogicCode.YES);
        List<CabinetStatisticsVO> list = aggregateService.searchCabinet(param);
        ExcelUtil<CabinetStatisticsVO> util = new ExcelUtil<>(CabinetStatisticsVO.class);
        util.exportExcel(response, list, "未绑定货柜数据");
    }


    @ApiOperation("货柜详细信息")
    @PreAuthorize("@ss.hasPermi('container:container:info')")
    @GetMapping(value = "/{containerId}")
    public Response<CabinetStatisticsVO> getInfo(@PathVariable("containerId") Long containerId) {
        return ResponseHelper.success(aggregateService.selectCabinet(containerId));
    }

    @PostMapping
    @ApiOperation("新增货柜信息")
    @PreAuthorize("@ss.hasPermi('container:container:add')")
    @Log(title = "货柜", businessType = BusinessType.INSERT)
    public Response<Object> create(@RequestBody CabinetDTO cabinet) {
        Long l = business.create(cabinet);
        return ResponseHelper.success().withDataId(l);
    }

    @PutMapping
    @ApiOperation("修改货柜信息")
    @PreAuthorize("@ss.hasPermi('container:container:edit')")
    @Log(title = "货柜", businessType = BusinessType.UPDATE)
    public Response<String> update(@RequestBody CabinetDTO cabinet) {
        business.update(cabinet);
        return ResponseHelper.success("修改成功");
    }

    @DeleteMapping("/{ids}")
    @ApiOperation("删除货柜信息")
    @PreAuthorize("@ss.hasPermi('container:container:remove')")
    public Response<String> remove(@PathVariable Long... ids) {
        business.delete(ids);
        return ResponseHelper.success("删除成功");
    }

    @GetMapping("/listBySiteId")
    @ApiOperation("根据网点ID查找网点下的货柜信息")
    public Response<List<CabinetStatisticsVO>> list(CabinetParam param) {
        List<CabinetStatisticsVO> list = aggregateService.searchCabinet(param);
        return ResponseHelper.successWithPage(list);
    }

    @ApiOperation("获取代理商下货柜信息")
    @GetMapping("/listByAgent")
    public Response<List<CabinetStatisticsVO>> listByAgent(CabinetParam param) {
        List<CabinetStatisticsVO> list = aggregateService.searchCabinet(param);
        return ResponseHelper.success(list);
    }

    @ApiModelProperty("批量设置商品")
    @PreAuthorize("@ss.hasPermi('container:container:product')")
    @PostMapping("/batchSetProduct")
    public Response<String> batchSetProduct(@RequestBody SetProductDTO dto) {
        business.batchSetProduct(dto);
        return ResponseHelper.success("操作成功");
    }


    @ApiOperation("批量设置商品商品查询")
    @GetMapping("/specificSearch")
    public Response<List<SpecificCabinetVO>> specificSearch(CabinetParam param) {
        List<SpecificCabinetVO> list = aggregateService.cabinetSpecificSearch(param);
        return ResponseHelper.success(list);
    }

    @ApiOperation("解绑货柜")
    @PreAuthorize("@ss.hasPermi('container:container:handlefree')")
    @PostMapping("/unbindCabinet")
    public Response<String> unbindCabinet(@RequestParam("containerId") Long containerId) {
        business.unbindCabinet(containerId);
        return ResponseHelper.success();
    }


    /**
     * ------------------------------------------搜索栏------------------------------------------
     */

    @ApiOperation("货柜名称匹配搜索")
    @GetMapping("/searchName")
    public Response<List<String>> searchName(@RequestParam("name") String name) {
        return ResponseHelper.successWithPage(business.searchName(name));
    }

    @ApiOperation("网点名称匹配搜索")
    @GetMapping("/searchSiteName")
    public Response<List<String>> searchSiteName(@RequestParam("siteName") String siteName) {
        return ResponseHelper.successWithPage(business.searchSiteName(siteName));
    }

    @ApiOperation("代理商名称匹配搜索")
    @GetMapping("/searchAgentName")
    public Response<List<String>> searchAgentName(@RequestParam("agentName") String agentName) {
        return ResponseHelper.successWithPage(business.searchAgentName(agentName));
    }
}
